﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework;

namespace Sudum
{
    class GameState_VideoPlayback : IGameState
    {
        private SudumVideo SVideo;
        private SpriteBatch spriteBatch;
        private FlowAction stateActionAfterDonePlaying;

        public GameState_VideoPlayback(string videoFile, FlowAction stateAfterDonePlaying)
        {
            this.spriteBatch = new SpriteBatch(Sudum.Me.GraphicsDevice);
            this.SVideo = new SudumVideo(Sudum.Me.Content.Load<Video>(videoFile)); // here goes the name of the video file 
            this.stateActionAfterDonePlaying = stateAfterDonePlaying;
        }

        public FlowAction Update()
        {
            //if (Sudum.Current().IsKeyPressed(Keys.Space))
            if (this.SVideo.State == MediaState.Playing)
            {
				bool skpVid = InputManager.Me.Pressed(Action.Exit) || InputManager.Me.Pressed(Action.Select);
                if (skpVid || this.SVideo.PlayedOnce)
                {
                    this.SVideo.StopVideo();
                    return stateActionAfterDonePlaying;
                }
            }
            else this.SVideo.PlayVideo();
            return FlowAction.FlowTo_Continue;
        }

        public void Render()
        {
            if (this.SVideo.State == MediaState.Stopped)
                return;
            Viewport vp = Sudum.Me.GraphicsDevice.Viewport;
            SVideo.DrawVideo(this.spriteBatch, vp.X, vp.Y, vp.Width, vp.Height);
        }
    }
}
